<?php
namespace app\data_magic\widget;

use app\data_magic\logic\Field as FieldLogic;
use app\data_magic\data\Field;
use think\Db;
use app\data_magic\struct\File;
use app\data_magic\struct\Location;


/**
 * 用来创建列表部件时需要的数据
 * @property array $fields 字段列表
 * @property array $data_list 数据列表
 */
class ListWidget extends Widget
{
    /**
     * @var array $_fields 原始的字段列表
     * @var array $_data_list 原始的数据列表
     * @var array $__data_list 将值处理成字符串的数据列表
     */
    protected $_fields=[];
    protected $_data_list=[];
    protected $__data_list=[];
    public function __set($name, $value)
    {
        if($name==="fields"){
            $this->_fields=$value;
            $this->__data_list=[];
        }
        elseif($name==="data_list"){
            $this->_data_list=$value;
            $this->__data_list=[];
        }
    }
    public function __get($name)
    {
        if($name==="fields"){
            return $this->_fields;
        }
        elseif($name==="data_list"){
            if(!$this->__data_list){
                $this->__data_list=$this->convertToString($this->_fields,$this->_data_list);
            }
            return $this->__data_list;
        }
    }

    /**
     * @var array $list_actions 全部数据操作列表
     */
    public $list_actions=[];

    /**
     * @var array $single_item_actions 单项数据操作列表
     */
    public $single_item_actions=[];

    /**
     * @var array $multi_item_actions 多项数据操作列表
     */
    public $multi_item_actions=[];

    /**
     * 设置关联的操作，并且将其分类
     * @param array $actions 操作列表
     */
    public function setActions($actions)
    {
        foreach($actions as $action){
            if($action["data_type"]==="single"){
                $this->single_item_actions[]=$action;
            }
            elseif($action["data_type"]==="multi"){
                $this->multi_item_actions[]=$action;
            }
        }
    }

    public function toArray()
    {
        $fields=[];
        foreach($this->fields as $field){
            $fields[]=$field->toArray();
        }
        return [
            "fields"=>$fields,
            "data_list"=>$this->data_list,
            "list_actions"=>$this->list_actions,
            "single_item_actions"=>$this->single_item_actions,
            "multi_item_actions"=>$this->multi_item_actions,
        ];
    }

    /**
     * 将列表中所有的值转换成相应的字符串
     * 返回$data_list
     * @param Field[] $fields 字段设置
     * @param array $list 数据列表
     * @return array
     */
    public function convertToString($fields,$list)
    {
        $fields=Field::parserFieldParams($fields);
        $result=[];
        foreach($list as $index => $item){
            $item=Field::convertFromStorageFormat($fields,$item);
            foreach($fields as $field){
                $field_name=$field->name;
                $value="";
                if(in_array($field->type,["select","radio","checkbox"])){
                    $map=[];
                    foreach($field->options as $option){
                        $map[$option->name]=$option->title;
                    }
                    $titles=[];
                    foreach($item[$field_name] as $name){
                        $titles[] = isset($map[$name]) ? $map[$name] :$name ;
                    }
                    $value = implode(" ",$titles);
                }
                elseif($field->type==="refer"){
                    $params=$field->refer;
                    $refer_ids=implode(",",$item[$field_name]);
                    $refer=Db::table($params->table_name)
                        ->field($params->id_field.",".$params->name_field)
                        ->where($params->id_field." in ($refer_ids)")->select();
                    $name_list=[];
                    foreach($refer as $refer_item){
                        $name_list[]=$refer_item[$params->name_field];
                    }
                    $value=implode(" ",$name_list);
                }
                elseif(in_array($field->type,["datetime"])){
                    $value=date("Y-m-d H:i:s",$item[$field_name]);
                }
                elseif(in_array($field->type,["date"])){
                    $value=date("Y年m月d日",$item[$field_name]);
                }
                elseif(in_array($field->type,["images","files"])){
                    if($item[$field_name]){
                        $uploaded=$item[$field_name];
                        $upload_names=[];
                        /** @var File $file */
                        foreach($uploaded as $file){
                            $upload_names[]=$file->name;
                        }
                        $value=implode(" ",$upload_names);
                    }
                }
                elseif($field->type==="location"){
                    if($item[$field_name]){
                        /** @var Location $location */
                        $location=$item[$field_name];
                        $value=$location->text;
                    }
                }
                else{
                    $value=$item[$field_name];
                }
                $result[$index][$field_name]=$value;
            }
        }
        return $result;
    }
}
